//
//  OneTableView.m
//  PerfectWorld
//
//  Created by dllo on 16/9/26.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "OneTableView.h"
#import "DecorateHeaderTableViewCell.h"
#import "DecorateSonViewController.h"
#import "NetworkHandler.h"
#import "RootModel.h"
#import "Projects.h"
#import "User.h"
#import "UserImage.h"

@interface OneTableView ()
@property (nonatomic, strong)RootModel *rootModel;
@property (nonatomic, strong)NSMutableArray *arr;
@end
@implementation OneTableView


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self handData];
        [self reloadData];
        self.delegate = self;
        self.dataSource = self;
        [self registerClass:[DecorateHeaderTableViewCell class] forCellReuseIdentifier:@"cellmessage"];
    }
    return self;
}
- (void)handData {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [user objectForKey:@"dic"];
    NSDictionary *dicUser = [dic objectForKey:@"user"];
    NSString *userName = [dicUser objectForKey:@"userName"];
    NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/projectlikes?start=0&count=5", userName];
    NSLog(@"%@", strURL);
    [NetworkHandler getDataByUrlString:strURL BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        self.rootModel = [RootModel modelWithDic:dic];
        [self reloadData];
    }];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DecorateSonViewController * VC = [[DecorateSonViewController alloc] init];
    VC.urlId = _rootModel.projects[indexPath.row].Son_id.description;
    [self.pushDelegate pushViewController:VC id:_rootModel.projects[indexPath.row].Son_id];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.bounds.size.height / 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rootModel.projects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DecorateHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellmessage"];
    cell.projectModel = _rootModel.projects[indexPath.row];
    return cell;
}

@end
